package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SjMeterReading;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.mapper.ElecMeterMapper;
import com.ruoyi.system.service.IElecMeterService;
import com.ruoyi.system.service.ISysFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ElecMeterServiceImpl implements IElecMeterService{

    @Autowired
    private ElecMeterMapper elecMeterMapper;

    @Autowired
    private ISysFloorService sysFloorService;

    @Override
    public List<SjMeterReading> getElecMeterList(SjMeterReading sjMeterReading) {
        return elecMeterMapper.getElecMeterList(sjMeterReading);
    }

    @Override
    public SjMeterReading getElecMeterById(String ammeterId) {
        return elecMeterMapper.getElecMeterById(ammeterId);
    }

    @Override
    public int insertElecMeter(SjMeterReading sjMeterReading) {
        //查询上期数据
        if(sjMeterReading.getDegreeIssueA()==null){
            sjMeterReading.setDegreeIssueA(0.0);
        }
        if(sjMeterReading.getDegreeIssueB()==null){
            sjMeterReading.setDegreeIssueB(0.0);
        }
        if(sjMeterReading.getDegreeIssueC()==null){
            sjMeterReading.setDegreeIssueC(0.0);
        }
        if(sjMeterReading.getDegreeIssueD()==null){
            sjMeterReading.setDegreeIssueD(0.0);
        }
        SjMeterReading sjmr = initSjmr(sjMeterReading);
        sjmr.setAmmeterId(UUID.randomUUID().toString().replaceAll("-",""));
        return elecMeterMapper.insertElecMeter(sjMeterReading);
    }

    @Override
    public int updateElecMeter(SjMeterReading sjMeterReading) {
        //查询上期数据
        if(sjMeterReading.getDegreeIssueA()==null){
            sjMeterReading.setDegreeIssueA(0.0);
        }
        if(sjMeterReading.getDegreeIssueB()==null){
            sjMeterReading.setDegreeIssueB(0.0);
        }
        if(sjMeterReading.getDegreeIssueC()==null){
            sjMeterReading.setDegreeIssueC(0.0);
        }
        if(sjMeterReading.getDegreeIssueD()==null){
            sjMeterReading.setDegreeIssueD(0.0);
        }
        SjMeterReading sjmr = initSjmr(sjMeterReading);
        return elecMeterMapper.updateElecMeter(sjmr);
    }

    @Override
    public int removeElecMeter(String ids) {
        String[] ammeterIds = Convert.toStrArray(ids);
        return elecMeterMapper.removeElecMeter(ammeterIds);
    }

    @Override
    public SjMeterReading selectElecMeterByFloorAndEtldt(SjMeterReading sjMeterReading) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String etldt = "";
        if(sjMeterReading.getIssueDate()!=null){
            etldt = sdf.format(sjMeterReading.getIssueDate());
        }
        sjMeterReading.setEtldt(etldt);
        return elecMeterMapper.selectElecMeterByFloorAndEtldt(sjMeterReading);
    }

    @Override
    public String importElecMeter(List<SjMeterReading> sjMeterReadingList, boolean updateSupport) {
        int size = 0;
        if(sjMeterReadingList!=null){
            size = sjMeterReadingList.size();
        }
        int doUp = 0;
        int noUp = 0;
        int errorData = 0;
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        StringBuilder doUpdates = new StringBuilder();
        StringBuilder noUpdates = new StringBuilder();
        StringBuilder errorDatas = new StringBuilder();
        for(SjMeterReading sjMeterReading:sjMeterReadingList){
            Date issueDate;
            try {
                issueDate = sdfDate.parse(sjMeterReading.getExcelTmpDate());
                sjMeterReading.setIssueDate(issueDate);
            } catch (ParseException e) {
                errorData++;
                String errStr = "缺少抄表日期或日期格式不正确；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(StringUtils.isEmpty(sjMeterReading.getFloorName())){
                errorData++;
                String errStr = "未填写楼层；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(sjMeterReading.getDegreeIssueA()==null){
                sjMeterReading.setDegreeIssueA(0.0);
            }
            if(sjMeterReading.getDegreeIssueB()==null){
                sjMeterReading.setDegreeIssueB(0.0);
            }
            if(sjMeterReading.getDegreeIssueC()==null){
                sjMeterReading.setDegreeIssueC(0.0);
            }
            if(sjMeterReading.getDegreeIssueD()==null){
                sjMeterReading.setDegreeIssueD(0.0);
            }
            String etldt = sdf.format(issueDate);
            sjMeterReading.setEtldt(etldt);
            //查询数据是否已有
            SjMeterReading thisSJMR = elecMeterMapper.selectElecMeterByFloorAndEtldt(sjMeterReading);
            if(thisSJMR!=null){  //本期数据已有
                SysFloor sysFloor =sysFloorService.selectSysFloorByName(thisSJMR.getFloorName());
                if(sysFloor!=null) {
                    if(updateSupport){  //更新
                        Double allDegree = sjMeterReading.getDegreeIssueA()+sjMeterReading.getDegreeIssueB()
                                +sjMeterReading.getDegreeIssueC();
                        if(!"1".equals(sjMeterReading.getFloorName())){
                            allDegree = allDegree + sjMeterReading.getDegreeIssueD();
                        }
                        sjMeterReading.setDegreeIssue(allDegree);
                        SjMeterReading sjmr = initSjmr(sjMeterReading);
                        sjmr.setAmmeterId(thisSJMR.getAmmeterId());
                        elecMeterMapper.updateElecMeter(sjmr);
                        doUp++;
                        String doUpdateStr = "月份：" + etldt + "，" + "楼层：" + thisSJMR.getFloorName() + "；</br>";
                        doUpdates.append(doUpdateStr);
                    }else{  //不更新
                        noUp++;
                        String noUpdateStr = "月份：" + etldt + "，" + "楼层：" + thisSJMR.getFloorName() + "；</br>";
                        noUpdates.append(noUpdateStr);
                    }
                }else{  //楼层不存在
                    errorData++;
                    String errStr = "楼层：" + thisSJMR.getFloorName() + "不存在；</br>";
                    errorDatas.append(errStr);
                }
            }else{  //本期数据未有
                Double allDegree = sjMeterReading.getDegreeIssueA()+sjMeterReading.getDegreeIssueB()
                        +sjMeterReading.getDegreeIssueC();
                if(!"1".equals(sjMeterReading.getFloorName())){
                    allDegree = allDegree + sjMeterReading.getDegreeIssueD();
                }
                sjMeterReading.setDegreeIssue(allDegree);
                SjMeterReading sjmr = initSjmr(sjMeterReading);
                SysFloor sysFloor =sysFloorService.selectSysFloorByName(sjmr.getFloorName());
                if(sysFloor!=null) {
                    sjmr.setAmmeterId(UUID.randomUUID().toString().replaceAll("-",""));
                    elecMeterMapper.insertElecMeter(sjmr);
                }else{  //楼层不存在
                    errorData++;
                    String errStr = "楼层：" + sjMeterReading.getFloorName() + "不存在；</br>";
                    errorDatas.append(errStr);
                }
            }
        }
        String message;
        if(size>0){
            message = "共导入"+size+"条数据。</br>";
        }else{
            return "无数据";
        }
        if(StringUtils.isNotEmpty(doUpdates.toString())){
            message = message + "其中" + doUp + "条数据已有，已全部更新，楼层批次如下：</br>";
            message = message + doUpdates.toString();
        }
        if(StringUtils.isNotEmpty(noUpdates.toString())){
            message = message + "其中" + noUp + "条数据已有，未更新，楼层批次如下：</br>";
            message = message + doUpdates.toString();
        }
        if(StringUtils.isNotEmpty(errorDatas.toString())){
            message = message + "其中" + errorData + "条数据导入失败，错误原因如下：</br>";
            message = message + errorDatas.toString();
        }
        return message;
    }

    public SjMeterReading initSjmr(SjMeterReading sjMeterReading){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date date = sjMeterReading.getIssueDate();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, - 1 );
        Date m = c.getTime();
        String oldEtldt = sdf.format(m);
        SjMeterReading oldSjmr = new SjMeterReading();
        oldSjmr.setFloorName(sjMeterReading.getFloorName());
        oldSjmr.setEtldt(oldEtldt);
        SjMeterReading oldSJMR = elecMeterMapper.selectElecMeterByFloorAndEtldt(oldSjmr);
        Double degreePreA = 0.0;
        Double degreePreB = 0.0;
        Double degreePreC = 0.0;
        Double degreePreD = 0.0;
        Double degreePre = 0.0;
        if(oldSJMR!=null){
            if(oldSJMR.getDegreeIssueA()!=null){
                degreePreA = oldSJMR.getDegreeIssueA();
            }
            if(oldSJMR.getDegreeIssueB()!=null){
                degreePreB = oldSJMR.getDegreeIssueB();
            }
            if(oldSJMR.getDegreeIssueC()!=null){
                degreePreC = oldSJMR.getDegreeIssueC();
            }
            if(oldSJMR.getDegreeIssueD()!=null){
                degreePreD = oldSJMR.getDegreeIssueD();
            }
            if(oldSJMR.getDegreeIssue()!=null){
                degreePre = oldSJMR.getDegreeIssue();
            }
        }
        sjMeterReading.setDegreePreA(degreePreA);
        sjMeterReading.setDegreePreB(degreePreB);
        sjMeterReading.setDegreePreC(degreePreC);
        sjMeterReading.setDegreePreD(degreePreD);
        sjMeterReading.setDegreePre(degreePre);
        sjMeterReading.setDegreeA(sjMeterReading.getDegreeIssueA()-degreePreA);
        sjMeterReading.setDegreeB(sjMeterReading.getDegreeIssueB()-degreePreB);
        sjMeterReading.setDegreeC(sjMeterReading.getDegreeIssueC()-degreePreC);
        sjMeterReading.setDegreeD(sjMeterReading.getDegreeIssueD()-degreePreD);
        sjMeterReading.setDegree(sjMeterReading.getDegreeIssue()-degreePre);
        String etldt = sdf.format(date);
        sjMeterReading.setEtldt(etldt);
        return sjMeterReading;
    }

    @Override
    public int changeElecMeterStatus(SjMeterReading sjMeterReading) {
        return elecMeterMapper.changeElecMeterStatus(sjMeterReading);
    }

}
